package q21_mergeTwoLists;

import static CommonClass.Common.*;

public class Solution {


    public static void main(String[] args) {
        ListNode l1 = generateList(new int[]{0, 5});
        ListNode l2 = generateList(new int[]{1, 2, 4});

        ListNode l = mergeTwoLists(l1, l2);
        printList(l);

    }

    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null || list2 == null) {
            return list1 == null ? list2 : list1;
        }

        ListNode res = new ListNode(0);
        ListNode head = res;
        while ((list1 != null) && (list2 != null)) {
            // res.val = Math.min(list1.val, list2.val);
            if (list1.val > list2.val) {
                res.next = new ListNode(list2.val);
                list2 = list2.next;
            } else {
                res.next = new ListNode(list1.val);
                list1 = list1.next;
            }
            res = res.next;
        }

        res.next = (list1 == null) ? list2 : list1;

        return head.next;
    }


}
